<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="字典名称" prop="const_name">
        <el-input v-model.trim="form.const_name" placeholder="请输入字典名称" />
      </el-form-item>
      <el-form-item label="字典代码" prop="const_code">
        <el-input
          v-model.trim="form.const_code"
          placeholder="不填写将自动生成"
        />
      </el-form-item>
      <el-form-item label="字典描述" prop="const_desc">
        <el-input v-model.trim="form.const_desc" placeholder="请输入描述" />
      </el-form-item>
      <!-- <el-form-item label="层级" prop="const_deep">
        <el-input v-model.trim="form.const_deep" />
      </el-form-item> -->
      <el-form-item label="状态" prop="status">
        <el-switch
          v-model="form.status"
          active-text="显示"
          :active-value="1"
          inactive-text="隐藏"
          :inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { doEdit } from '@/api/dict'
export default {
  name: 'DictEdit',
  data() {
    return {
      form: {
        const_deep: 1,
        status: 1,
      },
      rules: {},
      title: '',
      dialogFormVisible: false,
    }
  },
  created() {},
  methods: {
    async showEdit(row) {
      if (!row) {
        this.title = '添加'
      } else {
        console.log(row)
        this.title = '编辑'
        this.form.const_id = row.id
        this.form.const_name = row.constant_name
        this.form.const_code = row.constant_code
        this.form.const_desc = row.constant_desc
        this.form.const_deep = row.constant_deep
        this.form.status = row.constant_status
      }
      this.dialogFormVisible = true
    },
    close() {
      this.$refs['form'].resetFields()
      this.form = this.$options.data().form
      this.dialogFormVisible = false
    },
    save() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          const { data } = await doEdit(this.form)
          if (data.is_success) {
            this.$baseMessage('删除成功', 'success', 'vab-hey-message-success')
            this.$emit('fetch-data')
            this.close()
          }
        }
      })
    },
  },
}
</script>
